package cn.afactory.tygz.ui.base.views.search;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.controller.SearchController;
import cn.afactory.tygz.model.CompanyInfo;
import cn.afactory.tygz.model.CompanyInfoList;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.model.SnsTopicInfoList;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.activities.SearchActivity;
import cn.afactory.tygz.ui.base.entity.LoganHotWordEntity;
import cn.afactory.tygz.ui.base.entity.SearchHistoryEntity;

/**
 * Created by Kenneth on 15/9/10.
 */
public class SearchMainFragment extends BaseFragment {

    private SearchSourcePager mSearchSourcePager = SearchSourcePager.COMPANY;
    private SearchType mSearchType = SearchType.COMPANY;
    private ViewGroup mHotKeyWordAndHistoryPanel;
    private HotSearchView mHotSearchKeywordView;
    private SearchHistoryView mSearchHistoryView;
    private SearchController mController;

    public static SearchMainFragment newInstance() {
        SearchMainFragment smf = new SearchMainFragment();
        return smf;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SearchController.MSG_GET_HOT_COMPANY_OK:
                if (msg.obj instanceof CompanyInfoList) {
                    CompanyInfoList companyInfoList = (CompanyInfoList)msg.obj;
                    List<LoganHotWordEntity> hotWordList = new ArrayList<LoganHotWordEntity>();
                    if (companyInfoList.value.size() > 0){
                       for (CompanyInfo companyInfo:companyInfoList.value) {
                           LoganHotWordEntity entity = new LoganHotWordEntity();
                           entity.content = companyInfo.companyName;
                           entity.qz = 10;
                           hotWordList.add(entity);
                       }
                    }
                    onHotKeyWordLoaded(hotWordList);
                }
                break;
            case SearchController.MSG_GET_HOT_TOPIC_OK:
                if (msg.obj instanceof SnsTopicInfoList) {
                    SnsTopicInfoList snsTopicInfoList = (SnsTopicInfoList)msg.obj;
                    List<LoganHotWordEntity> hotWordList = new ArrayList<LoganHotWordEntity>();
                    if (snsTopicInfoList.value.size() > 0){
                        for (SnsTopicInfo snsTopicInfo:snsTopicInfoList.value) {
                            LoganHotWordEntity entity = new LoganHotWordEntity();
                            entity.content = snsTopicInfo.topicName;
                            entity.qz = 10;
                            hotWordList.add(entity);
                        }
                    }
                    onHotKeyWordLoaded(hotWordList);
                }
                break;
            case SearchController.MSG_GET_HOT_TOPIC_KO:

                break;
            case SearchController.MSG_GET_HOT_COMPANY_KO:

                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addTitleBar = false;
        mController = new SearchController(mContext, mUIHandler);
    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_search_main_layout, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onTitleCreate();
        initViews(view);
        startLoadData();
    }

    public void onTitleCreate() {

    }

    private void initViews(View view) {

        mHotKeyWordAndHistoryPanel = (ViewGroup) view.findViewById(R.id.search_hot_keyword_history_panel);

        mHotSearchKeywordView = (HotSearchView) view.findViewById(R.id.search_hot_keyword_view);
        mSearchHistoryView = (SearchHistoryView) view.findViewById(R.id.search_history_view);

        mHotSearchKeywordView.setOnItemViewClickListener(new HotSearchView.OnItemViewClickListener() {
            @Override
            public void onItemClick(LoganHotWordEntity loganHotWordEntity) {
                performSearch(loganHotWordEntity.content);
            }
        });

        mSearchHistoryView.setOnItemClickListener(new SearchHistoryView.OnViewClickListener() {
            @Override
            public void OnItemClick(View v, int position, SearchHistoryEntity item) {
                performSearch(item.content);
            }

            @Override
            public void onCacheClear() {
                SearchHistory.getInstance(mContext).clearHistory();
            }
        });

    }

    /**
     * @param content
     */
    private void performSearch(String content) {
        if (mActivity instanceof SearchActivity) {
            ((SearchActivity) mActivity).startSearch(mSearchType, content);
        }
    }

    /**
     * @param type
     */
    public void setSearchPagerAndType(SearchSourcePager searchSourcePager, SearchType type) {
        mSearchSourcePager = searchSourcePager;
        mSearchType = type;
    }

    /**
     * 开始加载数据
     */
    public void startLoadData() {
        if (SearchType.COMPANY == mSearchType) {
            mController.getHotCompanyList();
        } else {
            mController.getHotTopicList();
        }
    }

    /**
     * 热词列表加载完成
     *
     * @param hwList
     */
    private void onHotKeyWordLoaded(List<LoganHotWordEntity> hwList) {
        if (!hwList.isEmpty()) {
            mHotSearchKeywordView.setVisibility(View.VISIBLE);
            mHotSearchKeywordView.setData(hwList);
        } else {
            mHotSearchKeywordView.setVisibility(View.GONE);
        }
    }
}
